//
//  LJFActivityController.m
//  SevenZhou
//
//  Created by qianfeng on 15/9/14.
//  Copyright (c) 2015年 ljf. All rights reserved.
//

#import "LJFActivityController.h"
#import "LJFCustomCollectionCell.h"
#import "LJFHttpManager.h"
#import "LJFWedController.h"
#import "LJFDetailController.h"
#import "UMSocial.h"
#import "MBProgressHUD.h"


@interface LJFActivityController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,didSelectedDelegate,UMSocialUIDelegate>
{
    NSMutableArray *_giftArr;
    NSMutableArray *_activityArr;
    NSMutableArray *_partArr;
    
    MBProgressHUD *_HUD;
}

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *greenConstraint;

@property (weak, nonatomic) IBOutlet UICollectionView *myCollectionView;



@end

@implementation LJFActivityController

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self)
    {
        
        _giftArr = [[NSMutableArray alloc] init];
        _activityArr = [[NSMutableArray alloc] init];
        _partArr = [[NSMutableArray alloc] init];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self showProgress];
    
    [self loadDataFromNetWithParameter:URL_activity_parameter1];
    
}

- (void)showProgress
{
    _HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"正在努力加载中";
    
    [self.view addSubview:_HUD];
    
    [_HUD show:YES];
    
}

#pragma mark - 从网络加载数据

- (void)loadDataFromNetWithParameter:(NSString *)parameter
{
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_activity AndParameterStr:parameter success:^(id responseObj)
    {
        
        NSArray *arr1 = responseObj[@"data"][@"being"];
        NSArray *arr2 = responseObj[@"data"][@"end"];
        
        [_giftArr addObject:arr1];
        [_giftArr addObject:arr2];
        
        [_myCollectionView reloadData];
        
        [_HUD hide:YES];
        
    } failure:^(NSError *error)
    {
        NSLog(@"error == %@",error);
        
    }];
}

- (void)loadDataFromNetWithParameter1:(NSString *)parameter
{
    if (_activityArr.count != 0)
    {
        return;
    }
    
    [self showProgress];
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_activity AndParameterStr:parameter success:^(id responseObj)
     {

         
         NSArray *arr1 = responseObj[@"data"][@"being"];
         NSArray *arr2 = responseObj[@"data"][@"end"];
         
         [_activityArr addObject:arr1];
         [_activityArr addObject:arr2];
         
         [_myCollectionView reloadData];
         
         [_HUD hide:YES];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
         
     }];
}

- (void)loadDataFromNetWithParameter2:(NSString *)parameter
{
    if (_partArr.count != 0)
    {
        return;
    }
    
    LJFHttpManager *manager = [LJFHttpManager shareInstance];
    
    [manager getDataFromNetWithUrl:URL_activity AndParameterStr:parameter success:^(id responseObj)
     {         
         NSArray *arr1 = responseObj[@"data"][@"end"];
         [_partArr addObjectsFromArray:arr1];
         [_myCollectionView reloadData];
         
     } failure:^(NSError *error)
     {
         NSLog(@"error == %@",error);
         
     }];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -didSelectedDelegate

-(void)didSelected:(LJFCustomCollectionCell *)cell withUrlStr:(NSString *)url
{
    LJFWedController *wed = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFWedController"];
    
    wed.urlString =url;
    
    [self.navigationController pushViewController:wed animated:YES];
}

-(void)didSelected:(LJFCustomCollectionCell *)cell withIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dict = _partArr[indexPath.row];
    
    LJFDetailController *detail = [self.storyboard instantiateViewControllerWithIdentifier:@"LJFDetailController"];
    detail.goodsid = dict[@"goods_id"];
    
    [self.navigationController pushViewController:detail animated:YES];
}

-(void)didSelected:(LJFCustomCollectionCell *)cell UMSocialWith:(NSString *)text andImage:(UIImage *)image
{
        [UMSocialSnsService
         presentSnsIconSheetView:self
         appKey:nil
         shareText:text
         shareImage:image
         shareToSnsNames:@[UMShareToSina,UMShareToTencent,UMShareToRenren,UMShareToDouban,UMShareToWechatSession,UMShareToWechatTimeline,UMShareToWechatFavorite]
         delegate:self];
}
- (IBAction)buttonAction:(UIButton *)sender
{
    CGFloat constant = 0;
    
    if (sender.tag == 150)
    {
        constant = 0;
        _myCollectionView.contentOffset = CGPointMake(0, 0);
    }
    else if(sender.tag == 151)
    {
        constant = self.view.frame.size.width/3.0;
        _myCollectionView.contentOffset = CGPointMake(self.view.frame.size.width, 0);
        [self loadDataFromNetWithParameter1:URL_activity_parameter2];
    }
    else
    {
        constant = self.view.frame.size.width/3.0*2.0;
        _myCollectionView.contentOffset = CGPointMake(2*self.view.frame.size.width, 0);
        [self loadDataFromNetWithParameter2:URL_activity_parameter3];
    }
    
    [UIView animateWithDuration:2.0 animations:^
    {
        self.greenConstraint.constant = constant;
    }];
}

#pragma mark - UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LJFCustomCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LJFCustomCollectionCell" forIndexPath:indexPath];
    cell.mydelegate = self;
    
    cell.giftArr = _giftArr;
    cell.activityArr = _activityArr;
    cell.partArr = _partArr;
    cell.CollectionRow = indexPath.row;

    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(self.view.frame.size.width, self.view.frame.size.height - 110 - 49);
}

#pragma mark - scrollViewdelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGFloat offset = scrollView.contentOffset.x/3.0;
    self.greenConstraint.constant = offset;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self loadDataFromNetWithParameter1:URL_activity_parameter2];
    [self loadDataFromNetWithParameter2:URL_activity_parameter3];
}

@end
